package com.qf.work.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.work.entity.Workorder;
import com.qf.work.mapper.WorkorderMapper;
import com.qf.work.service.WorkorderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author cd
 * @version 1.0
 * @date 2021/10/6 18:30
 */
@Service
public class WorkorderServiceImpl implements WorkorderService {

    @Autowired
    private WorkorderMapper workorderMapper;

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void insert(Workorder workoederEntry) {
        int i = workorderMapper.insert(workoederEntry);
        if (i != 1) {
            System.out.println("添加工单失败");
            throw new RuntimeException("添加工单失败");
        }
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void updateById(Workorder workoederEntry) {
        int i = workorderMapper.updateByPrimaryKey(workoederEntry);
        if (i != 1) {
            System.out.println("添加工单失败");
            throw new RuntimeException("添加工单失败");
        }
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void deleteById(Integer id) {
        int i = workorderMapper.deleteByPrimaryKey(id);
        if (i != 1) {
            System.out.println("添加工单失败");
            throw new RuntimeException("添加工单失败");
        }
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Workorder findById(Integer id) {
        return workorderMapper.findById(id);
    }

    @Override
    public PageInfo<Workorder> findByWorkIdAndPage(Integer page, Integer rows, Integer projectId) {
        //1. 分页
        PageHelper.startPage(page,rows);

        //2. 查询
        List<Workorder> airList = workorderMapper.findByworkId(projectId);

        //3. 封装PageInfo
        PageInfo<Workorder> pageInfo = new PageInfo<>(airList);

        //4. 返回
        return pageInfo;
    }
}
